package com.en.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.en.response.AppException;
import com.en.response.ErrorCodeMsgEnum;
import com.en.model.User;
import com.en.service.UserService;
import com.en.controller.User.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.en.response.R;
import com.en.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 
 * @Author: wht
 * @Date: 2024-01-16
 */
@Slf4j
@RestController
@RequestMapping("/")
@Api(tags = {"操作" })
public class UserController {
@Autowired
UserService userService;

@ApiOperation("新增一个")
@PutMapping("/addOneUser")
public<T> R<T> addOneUser(@RequestBody UserAddDto addDto){
    User user =new User();
        BeanUtils.copyProperties(addDto, user);
        boolean res=userService.save(user);

        if(res){
        return R.successMsg("添加成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.ADD_ERROR);
        }
        }

@ApiOperation("删除一个")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true)}
)
@DeleteMapping("/deleteOneUser/{id}")
public<T> R<T> deleteOneUser(@PathVariable("id") Integer id){
        if(id==null){
        throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        boolean res=userService.removeById(id);

        if(res){
        return R.successMsg("删除成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.DELETE_ERROR);
        }
        }

@ApiOperation("修改一个信息")
@PatchMapping("/updateOneUser")
public<T> R<T> updateOneUser(@RequestBody UserUpdateDto updateDto){
    User user =new User();
        BeanUtils.copyProperties(updateDto, user);
        boolean res=userService.updateById(user);

        if(res){
        return R.successMsg("修改成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.UPDATE_ERROR);
        }
        }

@ApiOperation("通过id获取一个信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true)}
)
@GetMapping("/getOneUser/{id}")
public<T> R<T> getOneUser(@PathVariable("id") Integer id){
        if(id==null){
        throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
    User user=userService.getById(id);
        return R.successData(user);
        }

@ApiOperation("分页获取列表")
@PostMapping("/getUserList")
public<T> R<T> getUserList(@Valid @RequestBody UserSearchDto searchDto)throws IllegalAccessException{
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();

        Field[]fields=searchDto.getClass().getDeclaredFields();
        for(Field field:fields){
        String name=field.getName();
        if(name.equals("page_num")||name.equals("page_size"))continue;
        field.setAccessible(true); // 必须将字段设置为可访问
        Object value=field.get(searchDto);
        if(value!=null){
        queryWrapper.eq(name,value);
        }
        }

        Page<User> page=new Page<>(searchDto.getPage_num(),searchDto.getPage_size());
    userService.page(page,queryWrapper);

        HashMap<Object, Object> res=new HashMap<>();
        res.put("page_size",page.getSize());
        res.put("page_num",page.getCurrent());
        res.put("total",page.getTotal());
        res.put("list",page.getRecords());
        return R.successData(res);
        }

@ApiOperation("获取所有列表")
@GetMapping("/getAllUserList")
public<T> R<T> getAllUserList(){
        List<User> list=userService.list();

        return R.successData(list);
        }
        }
